#!/bin/bash
# vim:fdm=marker fmr=[[[,]]] cms=\ #%s

[[ -z $SOAR ]] && SOAR=/opt/soar
PATH=$PATH:$SOAR/common/bash/soar
. soar.sh

build()
{ #[[[
    local svr=$1

    rm -f _svc_map.c
    local args=$(awk '
BEGIN {
    FS="=";
    svc_map_c="_svc_map.c";
    printf("-Wall -g ");
}
/ *SRCS *=/ { src_num=split($2, a_src, " "); }
/ *SVCS *=/ { svc_num=split($2, a_svc, " "); }
/ *OBJS *=/ { obj_num=split($2, a_obj, " "); }
/ *LIBS *=/ { lib_num=split($2, a_lib, " "); }
END {
    printf(" -I'$SOAR'/common/c/include -I'$SVCENV_DIR'/sys/skeleton -I'$SVCENV_DIR'/src/include");
    printf(" -o '$SVCENV_DIR'/bin/svr_'$svr'");
    printf(" '$SVCENV_DIR'/sys/skeleton/svr_skeleton.a svr.c %s", svc_map_c);
    for(i=1; i<=src_num; i++)
    {
        printf(" %s", a_src[i]);
    }
    for(i=1; i<=obj_num; i++)
    {
        printf(" %s", a_obj[i]);
    }
    for(i=1; i<=lib_num; i++)
    {
        printf(" %s", a_lib[i]);
    }
    printf(" -L'$SVCENV_DIR'/lib -L'$SOAR'/common/c/lib");

    printf("#include <svr_skeleton.h>\n\n") >> svc_map_c;
    printf("char *SERVER_NAME=\"'$svr'\";\n\n") >> svc_map_c;
    for(i=1; i<=svc_num; i++)
    {
        printf("extern SVC_FUNC(%s);\n", a_svc[i]) >> svc_map_c;
    }
    printf("\n") >> svc_map_c;
    printf("struct svc_map SVC_MAP[]=\n{\n") >> svc_map_c;
    for(i=1; i<=svc_num; i++)
    {
        printf("    { \"%s\", SVC_FUNC_%s },\n", a_svc[i], a_svc[i]) >> svc_map_c;
    }
    printf("};\n") >> svc_map_c;
    printf("unsigned int SVC_MAP_SIZE=sizeof(SVC_MAP)/sizeof(struct svc_map);\n") >> svc_map_c;
}
' svr.def)
    if [[ -r $SVCENV_DIR/lib/libcm.a ]]
    then
        args="$args -lcm"
    fi
    args="$args -lsoar_g "
    local db=$(awk 'BEGIN { FS="="; } / *DB *=/ { gsub("^ *","",$2); gsub(" *$","",$2); print $2; }' svr.def | tr 'A-Z' 'a-z')
    db=${db:-mysql}
    local dblib
    case $db in
    oracle)
        case $OS in
        linux) dblib="-L$ORACLE_HOME/lib -lclntsh" ;;
        esac
        ;;
    mysql)
        case $OS in
        linux)
            case $CPU in
            i386|i686) dblib="-L/usr/lib/mysql" ;;
            x86_64) dblib="-L/usr/lib64/mysql" ;;
            esac
            ;;
        freebsd)
            dblib=/usr/local/lib/mysql
            ;;
        esac
        dblib="$dblib -lmysqlclient_r"
        ;;
    *)
        ;;
    esac
    args="$args $dblib -lcrypto -lpthread -lrt -lm"
    echo "$CC $args"
    $CC $args
    return $?
} #]]]

main()
{ #[[[
    if [[ -z $SVCENV_DIR ]]
    then
        echo "SVCENV_DIR not set"
        return 1
    fi
    if [[ -z $SVCENV_CNFG ]]
    then
        echo "SVCENV_CNFG not set"
        return 1
    fi

    if [[ ! -r svr.def || ! -r svr.h || ! -r svr.c ]]
    then
        echo "svr.def & svr.h & svr.c is required"
        return 1
    fi

    local svr=$(basename $PWD | tr 'a-z' 'A-Z')
    local svr_cnfg=$(cnfg_get_property $SVCENV_CNFG SERVER svr_${svr})
    if [[ -z $svr_cnfg ]]
    then
        echo "Server($svr) not set in $SVCENV_CNFG"
        return 1
    fi
    local db=$(cnfg_get_subproperty $SVCENV_CNFG SERVER svr_$svr DB)
    if [[ -z $db ]]
    then
        echo "Server($svr) in $SVCENV_CNFG has no DB"
        return 1
    fi
    local start=$(cnfg_get_subproperty $SVCENV_CNFG SERVER svr_$svr START)
    if [[ -n $start ]]
    then
        (( start=start ))
        if (( start <= 0 ))
        then
            echo "Server($svr) in $SVCENV_CNFG has invalid START($start)"
            return 1
        fi
    fi

    build $svr
    if (( $? != 0 ))
    then
        echo "Build $svr fail"
        return 1
    fi
} #]]]

OS=$(uname | tr '[:upper:]' '[:lower:]')
CPU=$(uname -m | tr '[:upper:]' '[:lower:]')

main $@
exit $?
